package com.dayuanit.dy15.atm.service.impl;

import com.dayuanit.dy15.atm.entity.User;
import com.dayuanit.dy15.atm.entity.WxUserInfo;
import com.dayuanit.dy15.atm.mapper.WxUserInfoMapper;
import com.dayuanit.dy15.atm.service.WxBindService;
import com.dayuanit.dy15.atm.wx.WxUserInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class WxBindServiceImpl implements WxBindService {

    @Autowired
    private WxUserInfoMapper wxUserInfoMapper;

    @Override
    public void bindUser(WxUserInfoDTO wxUserInfoDTO, User user) {
        WxUserInfo wxUserInfo = new WxUserInfo();
        wxUserInfo.setCity(wxUserInfoDTO.getCity());
        wxUserInfo.setCountry(wxUserInfoDTO.getCountry());
        wxUserInfo.setCreateTime(new Date());
        wxUserInfo.setHeadimgurl(wxUserInfoDTO.getHeadimgurl());
        wxUserInfo.setNickname(wxUserInfoDTO.getNickname());
        wxUserInfo.setOpenId(wxUserInfoDTO.getOpenid());
        wxUserInfo.setSex(String.valueOf(wxUserInfoDTO.getSex()));
        wxUserInfo.setUnionid(wxUserInfoDTO.getUnionid());
        wxUserInfo.setUserId(user.getId());
        wxUserInfoMapper.insert(wxUserInfo);
    }

    @Override
    public WxUserInfo getWxUserInfo(String openId) {
        return wxUserInfoMapper.getByOpenId(openId);
    }
}

